package es.javamyadmin.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.javamyadmin.beans.ListaTablesBean;
import es.javamyadmin.pojo.Mensaje;
import es.javamyadmin.pojo.Error;

/**
 * Servlet implementation class VaciarTablaServlet
 * @author Emilio Javier Castillo Gonzalez
 * @author Hector Delgado Duran
 */
public class VaciarTablaServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1552076569257219848L;

	/**
	 * Metodo que llama a doPost
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * Metodo que vacia una tabla
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ListaTablesBean listaTablesBean = new ListaTablesBean();
		
		listaTablesBean = (ListaTablesBean) request.getSession().getAttribute("listaTablesBean");
		
		String user = listaTablesBean.getUser();     
        String pass = listaTablesBean.getPass();
        String host = listaTablesBean.getHost();
        String port = listaTablesBean.getPort();
        String database = listaTablesBean.getDatabase();
        String table = request.getParameter("tabla");
        InputStream path = getServletContext().getResourceAsStream("/WEB-INF/properties/mensajes.properties");
        
        Connection conexion = null;
    	Statement estamento = null;
    	PreparedStatement preparada = null;
    	ResultSet resultado = null;
		
		Error error = null;
		
		if(conexion==null){
			try{
				Class.forName("com.mysql.jdbc.Driver");                
	      	    conexion=DriverManager.getConnection("jdbc:mysql://"+host+":"+port+"/"+database, user, pass);
	      	    
	      	    PreparedStatement pstm = conexion.prepareStatement("TRUNCATE TABLE "+table);
	      	    pstm.execute();	
	      	    
			} catch(SQLException e){
				e.printStackTrace();
				error = new Error(Mensaje.getMensaje(path,String.valueOf(e.getErrorCode())), e.getMessage(), "");
				System.out.println("Error en --> "+error);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}finally{
				if(resultado != null){
					try {
						resultado.close();
					} catch(SQLException e){
						e.printStackTrace();
					}
					resultado = null;
				}
				if(estamento != null){
					try {
						estamento.close();
					} catch(SQLException e){
						e.printStackTrace();
					}
					estamento = null;
				}
				if(preparada != null){
					try {
						preparada.close();
					} catch(SQLException e){
						e.printStackTrace();
					}
					preparada = null;
				}
				if(conexion != null){
					try {
						conexion.close();
					} catch(SQLException e){
						e.printStackTrace();
					}
					conexion = null;
				}
			}
		}
		

		if(error != null && error.getError() != null){
			request.setAttribute("error", error.getError());
			if(error.getDescripcion() != null){
				request.setAttribute("descripcion", error.getDescripcion().replace("'", "\\'"));
			}
		}else{
			request.setAttribute("ok", Mensaje.getMensaje(path, "5011"));
		}
		
		request.getRequestDispatcher("jsp/lista-tablas-bd.jsp?bd="+database).forward(request,response);
	}

}
